﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Data.SqlClient;

/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class StopList_Result
{
    /// <summary>
    /// 站牌ID
    /// </summary>
    [DataMember(Order = 0)]
    public int id { get; set; }
    /// <summary>
    /// 站牌名稱
    /// </summary>
    [DataMember(Order = 1)]
    public string name { get; set; }
    /// <summary>
    /// Map坐標
    /// </summary>
    [DataMember(Order = 2)]
    public string lat { get; set; }
    /// <summary>
    ///  Map坐標
    /// </summary>
    [DataMember(Order = 3)]
    public string lng { get; set; }
    /// <summary>
    /// 站牌位置
    /// </summary>
    [DataMember(Order = 4)]
    public int seq { get; set; }
    /// <summary>
    /// 去回程
    /// </summary>
    [DataMember(Order = 5)]
    public int gob { get; set; }

    /// <summary>
    /// 預估時間
    /// </summary>
    [DataMember(Order = 6)]
    public int estime { get; set; }
}
[DataContract]
class StopList_Bus {
    /// <summary>
    /// Map坐標
    /// </summary>
    [DataMember(Order = 0)]
    public string lat { get; set; }
    /// <summary>
    ///  Map坐標
    /// </summary>
    [DataMember(Order = 1)]
    public string lng { get; set; }
}
public partial class Service_StopList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["rtid"])  && !string.IsNullOrEmpty(Request.QueryString["city"]) && !string.IsNullOrEmpty(Request.QueryString["bsid"]) && !string.IsNullOrEmpty(Request.QueryString["gob"]))
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                IList<StopList_Result> list = new List<StopList_Result>();
                StopList_Result item = null;
                int seqNo = 0;
                //獲取當前公車位置
                using (SqlDataReader rs2 = db.getList(string.Format("select b.seqNo,b.latitude,b.longitude from busevent a left join stop b on b.id=a.StopID where a.busid='{0}' and a.goback={1} and a.routeid='{2}' and b.city='{3}'", Request.QueryString["bsid"], Request.QueryString["gob"], Request.QueryString["rtid"], Request.QueryString["city"])))
                {
                    if (rs2.Read())
                    {
                        seqNo=Convert.ToInt32(rs2["seqNo"]);

                        StopList_Bus bus = new StopList_Bus();
                        bus.lat=rs2["latitude"].ToString();
                        bus.lng=rs2["longitude"].ToString();
                        using (DataTable dt2 = db.getDataTable(string.Format("select b.Id,b.nameZh,b.latitude,b.longitude,b.seqNo,b.goBack,c.EstimateTime from PathDetail a left join Stop b on a.stopId=b.id  left join EstimateTime c on c.StopID=a.stopId where a.pathAttributeId='{0}' and b.goBack={1} and b.city='{2}' and b.seqNo>{3} order by seqNo", Request.QueryString["rtid"], Request.QueryString["gob"], Request.QueryString["city"], seqNo), "mess2"))
                        {
                            foreach (DataRow rs in dt2.Rows)
                            {
                                item = new StopList_Result();
                                item.name = rs["nameZh"].ToString();
                                item.id = Convert.ToInt32(rs["id"]);
                                item.lat = rs["latitude"].ToString();
                                item.lng = rs["longitude"].ToString();
                                item.seq = Convert.ToInt32(rs["seqNo"]);
                                item.gob = Convert.ToInt32(rs["goBack"]);
                                item.estime = Convert.ToInt32(rs["EstimateTime"])/60;
                                list.Add(item);
                            }
                        }
                        Response.Write(string.Format("{{\"stop\":{0},\"bus\":{1}}}", MyDb.JSON.stringify(list),MyDb.JSON.stringify(bus)));
                        list.Clear();
                        list = null;
                    }
                    rs2.Close();
                }
                
            }
        }
        Response.End();
    }
}
